﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="CdRom.cs" company="Matthias Friedrich">
//   Copyright © Matthias Friedrich 2009
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

namespace DeviceManagement
{
	using System;

	public sealed class CdRom
	{
		private CdRom()
		{
		}

		public static void EnableDigitalPlayback(DeviceInfoSet deviceInfoSet, DeviceInfo deviceInfo, bool enable, bool forceUnknown = false)
		{
			if (deviceInfoSet == null)
			{
				throw new ArgumentNullException("deviceInfoSet");
			}

			if (deviceInfo == null)
			{
				throw new ArgumentNullException("deviceInfo");
			}

			SP_DEVINFO_DATA deviceInfoData = deviceInfo.DeviceInfoData;
			bool isEnabled = IsDigitalPlaybackEnabled(deviceInfoSet, deviceInfo);

			if (enable && !isEnabled)
			{
				DeviceManagement.CdromEnableDigitalPlayback(deviceInfoSet.Handle, ref deviceInfoData, forceUnknown);
			}
			else if (!enable && isEnabled)
			{
				DeviceManagement.CdromDisableDigitalPlayback(deviceInfoSet.Handle, ref deviceInfoData);
			}
		}

		public static bool IsDigitalPlaybackEnabled(DeviceInfoSet deviceInfoSet, DeviceInfo deviceInfo)
		{
			if (deviceInfoSet == null)
			{
				throw new ArgumentNullException("deviceInfoSet");
			}

			if (deviceInfo == null)
			{
				throw new ArgumentNullException("deviceInfo");
			}

			SP_DEVINFO_DATA deviceInfoData = deviceInfo.DeviceInfoData;

			bool isEnabled = false;
			if (DeviceManagement.CdromIsDigitalPlaybackEnabled(deviceInfoSet.Handle, ref deviceInfoData, ref isEnabled) == Kernel32.ERROR_SUCCESS)
			{
				return isEnabled;
			}

			Kernel32.ThrowDeviceManagementExceptionFromLastError();
			return false;
		}
	}
}